﻿var cookieRegex = /Currency=(.*?)(;|$)/;
var queryRegex = /(&|\?)Currency=(.*?)(&|$)/;
if (cookieRegex.test(document.cookie))
{
  var currency = cookieRegex.exec(document.cookie)[1];
  if (queryRegex.test(document.location.href))
  {
    var result = queryRegex.exec(document.location.href);
    if (result[2] != currency)
    {

      if (currency != "USD" && currency != "" && currency != null)
        document.location = document.location.href.replace(queryRegex, result[1] + "Currency=" + currency + result[3])

      else
      {
        if (result[1] == "?" && result[3] == "&")
          document.location = document.location.href.replace(queryRegex, "?");
        else
          document.location = document.location.href.replace(queryRegex, result[3]);


      }
    }
  }
  else
  {

    if (currency != "USD" && currency != "" && currency != null)
    {
      if (document.location.search != null && document.location.search != "")
        document.location = document.location.href + "&Currency=" + currency;
      else
        document.location = document.location.href + "?Currency=" + currency;
    }
  }
} 
